#include <iostream>
#include <vector>
#include "idDataBase/idDataBase.h"

// 定义一个简单的数据类型用于测试
struct MyDataType {
    int id;
    float value;
};

int main() {
    try {
        // 测试用例：测试数据库初始化
        IdDatabase<MyDataType> db("test.db");
        if (db.GetRecordCount() != 0) {
            std::cerr << "Test Initialization failed: Expected 0 records, got " << db.GetRecordCount() << std::endl;
            return 1;
        }
        std::cout << "Test Initialization passed." << std::endl;
    // 测试用例：测试注册人员
    
        MyDataType data1 = {1, 1.1f};
        MyDataType data2 = {2, 2.2f};

        db.RegisterPerson("person1", data1);
        db.RegisterPerson("person2", data2);

        if (db.GetRecordCount() != 2) {
            std::cerr << "Test RegisterPerson failed: Expected 2 records, got " << db.GetRecordCount() << std::endl;
            return 1;
        }
        std::cout << "Test RegisterPerson passed." << std::endl;

    // 测试用例：测试查找人员
        std::vector<MyDataType> result1 = db.FindPerson("person1");
        std::vector<MyDataType> result2 = db.FindPerson("person2");

        if (result1.size() != 1 || result1[0].id != 1 || result1[0].value != 1.1f) {
            std::cerr << "Test FindPerson failed for person1. Result size: " << result1.size() << std::endl;
            if (!result1.empty()) {
                std::cerr << "Result: id=" << result1[0].id << ", value=" << result1[0].value << std::endl;
            }
            return 1;
        }
        if (result2.size() != 1 || result2[0].id != 2 || result2[0].value != 2.2f) {
            std::cerr << "Test FindPerson failed for person2. Result size: " << result2.size() << std::endl;
            if (!result2.empty()) {
                std::cerr << "Result: id=" << result2[0].id << ", value=" << result2[0].value << std::endl;
            }
            return 1;
        }
        std::cout << "Test FindPerson passed." << std::endl;

    // 测试用例：测试删除人员

        db.DeletePerson("person1");

        if (db.GetRecordCount() != 1) {
            std::cerr << "Test DeletePerson failed: Expected 1 record, got " << db.GetRecordCount() << std::endl;
            return 1;
        }

        std::vector<MyDataType> result = db.FindPerson("person1");
        if (!result.empty()) {
            std::cerr << "Test DeletePerson failed: person1 should be deleted." << std::endl;
            return 1;
        }
        std::cout << "Test DeletePerson passed." << std::endl;

    // 测试用例：测试获取所有人员信息

        db.RegisterPerson("person1", data1);

        std::vector<std::pair<std::string, MyDataType>> allPersons = db.GetAllPersons();

        if (allPersons.size() != 2) {
            std::cerr << "Test GetAllPersons failed: Expected 2 records, got " << allPersons.size() << std::endl;
            return 1;
        }
        // 打印所有人员信息
        for (const auto& person : allPersons) {
            std::cout << "Person ID: " << person.first << ", id: " << person.second.id << ", value: " << person.second.value << std::endl;
        }

        // if (allPersons[1].first != "person1" || allPersons[1].second.id != 1 || allPersons[1].second.value != 1.1f) {
        //     std::cerr << "Test GetAllPersons failed for person1. Person ID: " << allPersons[1].first << ", id: " << allPersons[1].second.id << ", value: " << allPersons[1].second.value << std::endl;
        //     return 1;
        // }

        // if (allPersons[0].first != "person2" || allPersons[0].second.id != 2 || allPersons[0].second.value != 2.2f) {
        //     std::cerr << "Test GetAllPersons failed for person2. Person ID: " << allPersons[0].first << ", id: " << allPersons[0].second.id << ", value: " << allPersons[0].second.value << std::endl;
        //     return 1;
        // }
        std::cout << "Test GetAllPersons passed." << std::endl;

    // 测试用例：测试删除所有记录

        db.DeleteAllRecords();

        if (db.GetRecordCount() != 0) {
            std::cerr << "Test DeleteAllRecords failed: Expected 0 records, got " << db.GetRecordCount() << std::endl;
            return 1;
        }
        std::cout << "Test DeleteAllRecords passed." << std::endl;
        std::cout << "All tests passed successfully." << std::endl;
        return 0;
    } catch (const std::exception& e) {
        std::cerr << "Exception caught: " << e.what() << std::endl;
        return 1;
    }
}